#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import abc
from .util import SysInfo
from ._util import StraitObject
from ._game_straits import GameStraitNeedCacheDir, GameStraitHasCacheDir, GameStraitNeedConfigDir, GameStraitHasConfigDir
from ._game import GameBase
from ._runner_straits import RunnerStrait, RunnerStraitNeedCacheDir, RunnerStraitHasCacheDir, RunnerStraitNeedConfigDir, RunnerStraitHasConfigDir


class RunnerBase(abc.ABC, StraitObject):

    def __init__(self):
        StraitObject.__init__(self, RunnerStrait)

    def _initialize(self, sys_info, game_id, game_obj):
        assert isinstance(sys_info, SysInfo)
        assert isinstance(game_obj, GameBase)

        if game_obj.has_strait(GameStraitNeedCacheDir):
            self._addStrait(RunnerStraitNeedCacheDir(game_id))
        if game_obj.has_strait(GameStraitHasCacheDir):
            self._addStrait(RunnerStraitHasCacheDir(game_obj))
        if game_obj.has_strait(GameStraitNeedConfigDir):
            self._addStrait(RunnerStraitNeedConfigDir(game_id))
        if game_obj.has_strait(GameStraitHasConfigDir):
            self._addStrait(RunnerStraitHasConfigDir(game_obj))

    @abc.abstractmethod
    def _prepare(self, context):
        pass

    @abc.abstractmethod
    def _processGameHint(self, hint):
        pass

    @abc.abstractmethod
    async def _run(self, item):
        pass

    @abc.abstractmethod
    def _cleanup(self, on_error=False):
        pass


class RunnerContext:

    def __init__(self, edit_or_run):
        self.edit_or_run = edit_or_run
